package com.ymatou.userregister.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userregister.service.ActivateCodeCall;
import com.ymatou.userregister.service.RegUserEmailActivateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

public class Ts_RegUserEmailActivate {
	private static RegUserEmailActivateCall regUserEmailActiveCall;
	private static ActivateCodeCall activateCodeCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("注册邮箱激活接口测试");
	}

	@Before
	public void caseUp() throws Exception {
		regUserEmailActiveCall = new RegUserEmailActivateCall();
		activateCodeCall = new ActivateCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(regUserEmailActiveCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RegUserEmailActivate_001() {
		Logger.start(true,"邮箱激活账号成功");
		try {
		    Logger.comment("注册用户");
			String email = TestDataManager.getPhoneNum()+"@emailactive.com";
			Map user = UserCallService.registerUser(email, TestDataManager.getPhoneNum(),null);
			
			Logger.comment("发送验证码");
			activateCodeCall.setData("id="+email);
			activateCodeCall.callService();
			
			Logger.comment("验证邮箱");
			Thread.sleep(1000);
			String activeParams = activateCodeCall.getReturnData().split("=")[1].replace("\"", "");
			regUserEmailActiveCall.setData("id="+activeParams);
			regUserEmailActiveCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, regUserEmailActiveCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals(user.get("userId").toString(), regUserEmailActiveCall.getString("Result"), "验证UserId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_RegUserEmailActivate_002() {
		Logger.start(false,"输入错误的验证码，失败");
		try {
		    Logger.comment("注册用户");
			String email = TestDataManager.getPhoneNum()+"@emailactive.com";
			UserCallService.registerUser(email, TestDataManager.getPhoneNum(),null);
			
			Logger.comment("发送验证码");
			activateCodeCall.setData("id="+email);
			activateCodeCall.callService();
			
			Logger.comment("验证邮箱");
			Thread.sleep(1000);
			String activeParams = activateCodeCall.getReturnData().split("=")[1].replace("\"", "");
			regUserEmailActiveCall.setData("id="+activeParams+"abc");
			regUserEmailActiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, regUserEmailActiveCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("签名验证错误", regUserEmailActiveCall.getString("LastErrorMessage"), "验证Msg信息");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_RegUserEmailActivate_003() {
		Logger.start(false,"未输入验证码，邮箱激活账号失败");
		try {
			regUserEmailActiveCall.setData("id=");
			regUserEmailActiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, regUserEmailActiveCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("错误的激活码", regUserEmailActiveCall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_RegUserEmailActivate_004() {
		Logger.start(false,"重复激活用户，失败");
		try {
		    Logger.comment("注册用户");
			String email = TestDataManager.getPhoneNum()+"@emailactive.com";
			Map user = UserCallService.registerUser(email, TestDataManager.getPhoneNum(),null);
			
			Logger.comment("发送验证码");
			activateCodeCall.setData("id="+email);
			activateCodeCall.callService();
			
			Logger.comment("验证邮箱");
			Thread.sleep(1000);
			String activeParams = activateCodeCall.getReturnData().split("=")[1].replace("\"", "");
			regUserEmailActiveCall.setData("id="+activeParams);
			regUserEmailActiveCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, regUserEmailActiveCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals(user.get("userId").toString(), regUserEmailActiveCall.getString("Result"), "验证UserId");
			
			regUserEmailActiveCall.setData("id="+activeParams);
			regUserEmailActiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, regUserEmailActiveCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", regUserEmailActiveCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("用户已激活", regUserEmailActiveCall.getString("LastErrorMessage"), "验证Msg信息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}
